#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>

#include "reznames.h"

void *RezPointers[509];
void *ArtPtr;
void *ASPtr;
int QuitFlag = 0;
char Inventory[19];
int IconPic[7];

int FrameStop;
int GameMode;
int DaelonCntr;
int StatPlay;
int Spell1;
int Spell2;
int Spell3;
int ItemInhibit;
int ASMode;
int ATimerFlag;
int BTimerFlag;
int CTimerFlag;
int SkylarTimerFlag;
int ATimer;
int BTimer;
int CTimer;
int SkylarTimer;
int TideCntl;
int MapEx;
int Health;
int HealthOld;
int HealthTmr;
int TreeEyeTimer;
int TreeHGTimer;
int TreeHGUPic;
int TreeLeftPic;
int TreeRightPic;
int TreeRightSta;
int TSIconOnly;
int Help;
int Eye;
int Replay;
int Pouch;
int NoIFScreen;
int NoMusic;
int FstFwd;
int GameMode;

void str_toupper(char *str) {
	int c = 0;
	while (str[c]) {
		str[c] = toupper(str[c]);
		++c;
	}
}

void* LoadAResource(int reznum) {
	// Weird off-by-one here?
	reznum--;

	void *p;
	char* path = strdup(RezNames[reznum]);
	str_toupper(path);
	printf("Loading resource: %i (%s)\n", reznum, path);

	if(! RezPointers[reznum]) {
		FILE *f = fopen(path, "rb");
		if(! f) {
			perror("Failed to open file");
			exit(1);
		}

		fseek(f, 0L, SEEK_END);
		int size = ftell(f);
		rewind(f);

		p = malloc(size);
		fread(p, size, 1, f);
		RezPointers[reznum] = p;
		fclose(f);
	}

	free(path);

	return p;	
}

void ReleaseAResource(int reznum) {
	// Weird off-by-one here?
	reznum--;

        char* path = strdup(RezNames[reznum]);
        str_toupper(path);

	printf("Freeing resource: %i (%s)\n", reznum, path);
	free(RezPointers[reznum]);
	RezPointers[reznum] = NULL;

	free(path);
}

void SetupPics() {
	// Well, this seems to be all...
	ArtPtr = LoadAResource(0x97); // Pics\kingArt.art
}

void ShowPic(int reznum) {
	printf("Showing picture: %i\n", reznum);
	// EraseCursor()
	void *resource = LoadAResource(reznum);
	// pal = ParseLBM(resource, "CMAP")
	// SetPaletteRGB(pal)
	// body = ParseLBM(resource, "BODY")
	// DrawFullPicPack(body)
	ReleaseAResource(reznum);
}

void InitTools() {
	// GoMCGA() 
	// InitMouse()
	// StartTimers()
	// FadeToBlack2()
	ShowPic(0x7C); // Pics\KLogo.lbm
}

void TitlePage() {
	// FadeToBlack2()
	// PlayMovie(0xC8)
	// FadeToBlack2()
	// PlayMovie(0xCE)
	// FadeToBlack2()
	// PlayMovie(0xC6)
}

void EnAll() {
	Help = 1;
	Eye = 1;
	Replay = 1;
	Pouch = 1;
	FstFwd = 1;
}

void InitPlay() {
	memset(Inventory, 0, sizeof(Inventory));
	memset(ArtPtr + 4, 0, 0xC6);

	for (int i = 0; i < 7; ++i) {
		IconPic[i] = 0x59 + i;
	}

	FrameStop = 0;
	GameMode = 0;
	DaelonCntr = 0;
	StatPlay = 0x0A;
	Spell1 = 0;
	Spell2 = 0;
	Spell3 = 0;
	ItemInhibit = 0;
	ASMode = 0;
	ATimerFlag = 0;
	BTimerFlag = 0;
	CTimerFlag = 0;
	SkylarTimerFlag = 0;
	ATimer = 0;
	BTimer = 0;
	CTimer = 0;
	SkylarTimer = 0;
	TideCntl = 0;
	MapEx = 0;
	Health = 0x0C;
	HealthOld = 1;
	HealthTmr = 0;
	TreeEyeTimer = 0;
	TreeHGTimer = 0;
	TreeHGUPic = 0x93;
	TreeLeftPic = 0;
	TreeRightPic = 0;
	TreeRightSta = 1;
	TSIconOnly = 0;

	EnAll();
	Pouch = 1;

	// Why here? Why NOT here
	srand(time(NULL));

	NoIFScreen = 1;
	NoMusic = 0;
	FstFwd = 1;

	free(ASPtr);
	ASPtr = 0;
}

void InitHelp() {
	GameMode = 0;
}

void GameHelp() {
	if (GameMode == 0)
		return;

	// Do help screen
}

void GPLogic1() {
	printf("GPLogic1()\n");

	if (StatPlay == 0x79) {
		// Switch on UserInput
	}
	
}

void DrawIcon(int offset, int width, void* data) {
	// VGA mode 0x13 encoded bitmap
}

int main() {
	chdir("game");
	SetupPics();
	InitTools();
	TitlePage();

	// while (QuitFlag == 0) {
	InitPlay();
	InitHelp();

	GameHelp();
	// if (GameMode == 0)
	// 	continue;
	//
	if (StatPlay < 0xFA)
		GPLogic1();

		
	// }
}

